Conversation
| questions: Joi.array() | ||
| .required() | ||
| .items( | ||
| Joi.object({ |
There was a problem hiding this comment.
It would be cool if this declaration would be in a separate file (maybe QuestionSchemas could fit) and reused on dependentQuestions instead of duplicating 😬
Also, I have read the README and it looks like there are different kind of questions with different properties. It would be amazing if each kind of question will have it own schema.
Something like this:
// baseQuestionSchema.js
const question = Joi.object({
// This way it can have recursive validations
dependentQuestion: Joi.array().items(Joi.lazy(() => question))
})
// checkBoxSchema.js
import questionSchema from './baseQuestionSchema.js'
const checkBoxSchema = questionSchema.append({
// checkBox specific properties
})
// questionsSchema.js
const questionsSchema = Joi.array().items(CheckBoxSchema, CountrySchema, InputSchema, ...etc)
// jsonValidator.js
questions: questionsSchema.required()This way, each Question update would be independent of the others and they would be easier to test 😬
What do you believe?
There was a problem hiding this comment.
I'm loving this approach, so well structured!
There was a problem hiding this comment.
I've changed the approach since Joi.lazy() and Joi.link() were not a solution, however I still have a bug I'm currently trying to solve
| isoCode, | ||
| ...props | ||
| }) => { | ||
| console.log(schema.validate(form)) |
There was a problem hiding this comment.
Maybe it would be better if instead of logging an error it will throw in case the schema is incorrect, what do you think?
There was a problem hiding this comment.
Absolutely, this was only temporary until we decide what would be rendered in case of error
| id: Joi.string().required() | ||
| }) | ||
|
|
||
| export default schema |
There was a problem hiding this comment.
Cause this file is a jsonValidator, what if instead of returning Joi's schemas it returns a validate function which uses Joi.validate. This way we'll be using a Joi's wrapper and we could change the library when required.
Something like this:
const validate = (json) => {
const result = schema.validate(json);
if (result.error) {
throw new Error(`Validation errors: ${result.error}`)
}
return result.value
}
ismaelocaramelo
left a comment
There was a problem hiding this comment.
Nice work @sofisdev I 've left some comments that would be nice to go through them. :)
| isoCode='ES' | ||
| onLinkOpen={onLinkOpen} | ||
| isLoading={isLoading} | ||
| validateJSON |
There was a problem hiding this comment.
We should avoid truly expressions. The reason behind this is if I'm passing to boolean expressions let say validateJSON and notValidateJSON I would not know the behavior without going to the implementation details. Think components as functions, passing booleans as arguments makes the function really hard to maintain. I'd use some sort of state handling @sofisdev
| let mockHandler = null | ||
| // const isLoading = false | ||
| let loaderc = false | ||
| // function useLoading(isLoading) { |
| const useFormObj = useForm({ defaultValues: { formatDate: '' } }) | ||
| const { errors } = useFormObj | ||
|
|
||
| validateJSON && validate(form) |
There was a problem hiding this comment.
Should we handle this with try and catch?
|
Branch not touched for 4 years. |
Type:
What's the focus of this PR:
FormBuildercomponent